package bootstrap

import (
	"fmt"
	"github.com/iris-contrib/swagger/v12"
	"github.com/iris-contrib/swagger/v12/swaggerFiles"
	"github.com/kataras/iris/v12"
	"micro-grpc-server/app/conf"
)

type Server struct {
	App  *iris.Application
	Host string
}

func NewServer(app *iris.Application) *Server {
	return &Server{
		App: app,
	}
}

func (s *Server) Run() (err error) {
	s.registerSwagger()

	s.App.Logger().SetLevel(conf.Instance.App.LogLevel)
	s.Host = fmt.Sprintf("%s:%d", conf.Instance.App.Host, conf.Instance.App.Port)
	if err := s.App.Run(
		iris.Addr(s.Host),
		iris.WithConfiguration(iris.YAML("config/app/iris.yaml")),
		iris.WithoutInterruptHandler,
	); err != nil {
		return err
	}
	return nil
}

func (s *Server) registerSwagger() {
	c := &swagger.Config{
		URL: "http://localhost:8877/swagger/doc.json",
	}
	s.App.Get("/swagger/{any:path}", swagger.CustomWrapHandler(c, swaggerFiles.Handler))
}
